
package control.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.AccountDAO;
import dao.CharacterDAO;
import dao.LogDAO;

/**
 * Servlet implementation class Account
 */
public class Account extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private AccountDAO			accountDAO;
	private CharacterDAO		characterDAO;
	private LogDAO				logDAO;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Account() {
		super();
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init( ServletConfig config ) throws ServletException {
		accountDAO = (AccountDAO) config.getServletContext().getAttribute(
				"AccountDAO" );
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
		logDAO = (LogDAO) config.getServletContext().getAttribute( "LogDAO" );
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		if( action.equals( "logout" ) ) {
			HttpSession session = request.getSession();
			session.removeAttribute( "ID" );
			goTo( "index.jsp", request, response );
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		/*
		 * account login section
		 */
		if( action.equals( "login" ) ) {
			int accountID = accountDAO.getAccountByLogin( request
					.getParameter( "loginName" ) );
			boolean accepted = accountDAO.checkLogin( accountID, request
					.getParameter( "password" ) );
			logLoginAction( accountID, accepted, request );
			if( accepted ) {
				HttpSession session = request.getSession();
				session.setAttribute( "ID", accountID );
				request.setAttribute( "character", characterDAO
						.getCharacterByID( accountID ) );
				goTo( "WEB-INF/logged/welcome.jsp", request, response );
			} else {
				goTo( "index.jsp", request, response );
			}
		}
		/*
		 * account register section
		 */
		if( action.equals( "register" ) ) {
			System.out.println( request.getParameter( "password_1" ) );
			accountDAO.addAccount( request.getParameter( "charName" ), request
					.getParameter( "realName" ), request
					.getParameter( "password_1" ), request
					.getParameter( "email" ) );
			goTo( "index.jsp", request, response );
		}
	}

	private void goTo( String action, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher( action );
		dispatcher.forward( request, response );
	}

	private void logLoginAction( int accountID, boolean accepted,
			HttpServletRequest request ) {
		logDAO.logLoginAction( accountID, accepted, request.getRemoteAddr(),
				getCurrentTime(), request.getHeader( "User-Agent" ) );
	}

	private Date getCurrentTime() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}
}
